﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using BoCaiWeb.Model;
using BoCaiWeb.Business;
using BoCaiWeb.WebSite.Code;
using Ajeelee.Core.Utils;

namespace BoCaiWeb.WebSite.WebAdmin
{
    public partial class UpdatePwd : AuthPageBase
    {
        Hashtable table = new Hashtable();
        protected UAdministrator userInfo = new UAdministrator();
        IUAdministratorBusiness userBll = new UAdministratorBusinessFactory().CreateUAdministratorBusiness();

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            try
            {
                userInfo = userBll.GetByName(User.Identity.Name);
                if (userInfo == null)
                {
                    userInfo = new UAdministrator();
                    MessageBoxGo("不存在该信息", "index.aspx");
                }
           
                if (IsPost)
                {
                    // 更新
                    if (userInfo.Id > 0)
                    {
                        int rel = 0;

                        if (userInfo.Pwd == FormParam("oldpassword").MD5Secret())
                        {
                            if (FormParam("newpassword") == FormParam("dnewpassword"))
                            {
                                table.Clear();
                                table.Add(UAdministratorHash.Pwd, FormParam("newpassword").MD5Secret());
                                table.Add(UAdministratorHash.Id, userInfo.Id);
                                rel = userBll.UpdateSomeVal(table);
                            }
                            else
                            {
                                MessageBox("两次密码不一样！");
                                return;
                            }
                        }
                        else
                        {
                            MessageBox("老密码密码错误！");
                            return;
                        }

                        if (rel > 0)
                        {
                            System.Threading.Thread.Sleep(500);
                            Page_GoTo("SysUsers.aspx", true);
                        }
                        else
                        {
                            MessageBox("修改失败！");
                            return;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Function.WriteErrorLog(ex);
            }
        }
    }
}